<template>
  <el-dialog v-model="dialogTableVisible" :title="title" width="90%">
    <div class="contract_base_title">{{ t('keys.Evaluation_detail') }}</div>
    <merge-table
      ref="mergeTableRef"
      style="margin-bottom: 10px"
      :showIndex="false"
      :tableData="tableList"
      :checkbox="false"
      :mergeFields="mergeFields"
      :column="selectVendorListRef"
      :actionBtnIsShow="false"
    >
    </merge-table>
    <div v-if="['INSPECT', 'ACCESS'].includes(tmplType)" class="contract_base_title mt_20">
      {{ tmplType == 'ACCESS' ? t('keys.Access_result_score_configuration') : t('keys.Investigate_the_result_score_configuration') }}
    </div>
    <merge-table
      v-if="['INSPECT', 'ACCESS'].includes(tmplType)"
      :isPreview="false"
      :showIndex="false"
      :column="tmplType == 'INSPECT' ? inspectColumnList : accessColumnList"
      :tableData="resultVoList"
      :showActionBtn="false"
      :checkbox="false"
      :actionBtnIsShow="false"
    >
    </merge-table>
  </el-dialog>
</template>
  <script setup>
import { ref, nextTick } from 'vue'
import MergeTable from '@/components/MergeTable.vue'
import { useMock } from '../hooks/mock.js'
import { getExamineTmplByIdApi } from '@/api/personalCenter'
import { ERR_OK } from '@/config/statusCode'
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
const { selectVendorList, inspectColumnList, accessColumnList } = useMock()
const selectVendorListRef = ref(selectVendorList)
const dialogTableVisible = ref(false)
const title = ref(t('keys.Preview_the_evaluation_template'))
const mergeTableRef = ref()
const tableList = ref([])
const resultVoList = ref([])
const tmplType = ref()
let mergeFields = ref(['quotaTypeName', 'quotaTypeWeight'])
const props = defineProps({})
let loading = ref(false)

//展示
const showDialog = (id) => {
  dialogTableVisible.value = true
  handelScoringTableList(id)
  // 注意： 根据考核模版的权重类型列表是否展示 指标类别权重、指标权重
}
// 获取数据
const handelScoringTableList = async (id) => {
  const { code, data } = await getExamineTmplByIdApi(id)
  if (code == ERR_OK) {
    if (data.contentVoList?.length) {
      tableList.value = data.contentVoList
      resultVoList.value = data.resultVoList || []
      resultVoList.value.forEach((t) => {
        if (t.numberStart == 0) {
          t.numberStart = '0'
        }
        if (t.numberEnd == 0) {
          t.numberEnd = '0'
        }
      })
      tmplType.value = data.tmplType
      console.log(resultVoList)
    }
    let data_ = {
      fieldCode: 'quotaWeight',
      fieldName: t('keys.Indicator_weight') + '（%）',
      visableFlag: true,
      visableReadonlyFlag: false,
      filterFlag: true,
      filterReadonlyFlag: false,
      exportFlag: true,
      exportReadonlyFlag: false,
      displayPage: ['NEW_PAGE', 'MODIFY_PAGE', 'CHANGE_PAGE']
    }

    let tmp = {
      fieldCode: 'quotaTypeWeight',
      fieldName: t('keys.Indicator_category_weight') + '（%）',
      visableFlag: true,
      visableReadonlyFlag: false,
      filterFlag: true,
      filterReadonlyFlag: false,
      exportFlag: true,
      exportReadonlyFlag: false,
      displayPage: ['NEW_PAGE', 'MODIFY_PAGE', 'CHANGE_PAGE'],
      displayFlag: true
    }
    const quatoIndex = selectVendorListRef.value.findIndex((v) => v.fieldCode == 'quotaWeight')
    const weightIndex = selectVendorListRef.value.findIndex((v) => v.fieldCode == 'quotaTypeWeight')
    if (data.weightType == '10') {
      if (quatoIndex >= 0) {
        selectVendorListRef.value.splice(quatoIndex, 1)
      }
      if (weightIndex >= 0) {
        selectVendorListRef.value.splice(weightIndex, 1)
      }
      mergeFields.value = ['quotaTypeName']
    }
    if (data.weightType == '20') {
      if (quatoIndex < 0) {
        selectVendorListRef.value.splice(2, 0, data_)
      }
      if (weightIndex >= 0) {
        selectVendorListRef.value.splice(weightIndex, 1)
        // mergeFields.value = ['quotaTypeName', 'quotaWeight']
        mergeFields.value = ['quotaTypeName']
      }
    }
    if (data.weightType == '30') {
      if (quatoIndex < 0) {
        selectVendorListRef.value.splice(2, 0, data_)
      }
      if (weightIndex < 0) {
        selectVendorListRef.value.splice(1, 0, tmp)
      }
      // mergeFields.value = ['quotaTypeName', 'quotaWeight', 'quotaTypeWeight']
      mergeFields.value = ['quotaTypeName', 'quotaTypeWeight']
    }

    nextTick(() => {
      mergeTableRef.value.toMergeColumn()
    })
  }

  loading.value = true
}

defineExpose({ showDialog })
</script>
<style lang="scss" scoped>
.mt_20 {
  margin-top: 20px
}
</style>
  